package zhr.chapter5.zuoye;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 用户输入形状类型和参数
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入形状类型或数字 1:Circle(圆形), 2:Rectangle(矩形), 3:Triangle(三角形) 0:退出");
            String shapeType = scanner.nextLine();
            if ("0".equals(shapeType)) {
                System.out.println("程序已退出");
                break;
            }
            double radius, length, width, side1, side2, side3;
            switch (shapeType) {
                case "Circle", "1" -> {
                    System.out.println("请输入半径:");
                    radius = scanner.nextDouble();
                    scanner.nextLine(); // 读取并丢弃换行符

                    Shape circle = new Circle(radius);
                    System.out.println("圆形的面积: " + String.format("%.2f", circle.area()));
                    System.out.println("圆形的周长: " + String.format("%.2f", circle.perimeter()));
                }
                case "Rectangle", "2" -> {
                    System.out.println("请输入长度:");
                    length = scanner.nextDouble();
                    scanner.nextLine(); // 读取并丢弃换行符

                    System.out.println("请输入宽度:");
                    width = scanner.nextDouble();
                    scanner.nextLine(); // 读取并丢弃换行符

                    Shape rectangle = new Rectangle(length, width);
                    System.out.println("矩形的面积: " + String.format("%.2f", rectangle.area()));
                    System.out.println("矩形的周长: " + String.format("%.2f", rectangle.perimeter()));
                }
                case "Triangle", "3" -> {
                    System.out.println("请输入第一条边:");
                    side1 = scanner.nextDouble();
                    scanner.nextLine(); // 读取并丢弃换行符

                    System.out.println("请输入第二条边:");
                    side2 = scanner.nextDouble();
                    scanner.nextLine(); // 读取并丢弃换行符

                    System.out.println("请输入第三条边:");
                    side3 = scanner.nextDouble();
                    scanner.nextLine(); // 读取并丢弃换行符

                    // 判断是否能构成三角形
                    if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1) {
                        System.out.println("这三条边不能构成三角形");
                    } else {
                        Shape triangle = new Triangle(side1, side2, side3);
                        System.out.println("三角形的面积: " + String.format("%.2f", triangle.area()));
                        System.out.println("三角形的周长: " + String.format("%.2f", triangle.perimeter()));
                    }
                }
                default -> System.out.println("未知的形状类型");
            }
        }
        scanner.close();
    }
}
